// pages/earns/earns.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: [],
    extract_time: 0,
    receive_time: 0,

    flag_rule: true,
    flag_ruleTwo: true,
    flag_concern: false,

    flag_out: true,
    flag_canvas:true,
    flag_share:true,
  },
  getTab() {
    wx.request({
      url: app.globalData.url + 'api/timing/applet/show',
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let is_show = e.data.data.is_show,
          tel = wx.getStorageSync('reg_telephone');
        // if (tel == '18258151021') {
        if (is_show == '2') {
          this.setData({
            tabbar: {
              color: "#fff",
              selectedColor: "#d0a76c",
              backgroundColor: "#1b1b1b",
              borderStyle: "black",
              list: [{
                  pagePath: "../../pages/industry/industry",
                  text: "行业",
                  iconPath: "../../images/icon/icon_ind.png",
                  selectedIconPath: "../../images/icon/icon_indS.png",
                  selected: false
                },
                {
                  pagePath: "../../pages/mine/mine",
                  text: "我的",
                  iconPath: "../../images/icon/icon_mine.png",
                  selectedIconPath: "../../images/icon/icon_mineS.png",
                  selected: false
                }
              ],
              position: "bottom"
            },
          })
          this.editTabBar();
        } else {
          if (this.data.role == 2) {
            this.setData({
              tabbar: {
                color: "#fff",
                selectedColor: "#d0a76c",
                backgroundColor: "#1b1b1b",
                borderStyle: "black",
                list: [{
                    pagePath: "../../pages/recruit/index/index",
                    text: "招聘",
                    iconPath: "../../images/icon/icon_rec.png",
                    selectedIconPath: "../../images/icon/icon_recS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/earns/earns",
                    text: "赚钱",
                    iconPath: "../../images/icon/icon_money.png",
                    selectedIconPath: "../../images/icon/icon_moneyS.png",
                    selected: true
                  },
                  {
                    pagePath: "../../pages/industry/industry",
                    text: "行业",
                    iconPath: "../../images/icon/icon_ind.png",
                    selectedIconPath: "../../images/icon/icon_indS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/mine/mine",
                    text: "我的",
                    iconPath: "../../images/icon/icon_mine.png",
                    selectedIconPath: "../../images/icon/icon_mineS.png",
                    selected: false
                  }
                ],
                position: "bottom"
              },
            })
          } else {
            this.setData({
              tabbar: {
                color: "#fff",
                selectedColor: "#d0a76c",
                backgroundColor: "#1b1b1b",
                borderStyle: "black",
                list: [{
                    pagePath: "../../pages/recruit/index/index",
                    text: "招聘",
                    iconPath: "../../images/icon/icon_rec.png",
                    selectedIconPath: "../../images/icon/icon_recS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/industry/industry",
                    text: "行业",
                    iconPath: "../../images/icon/icon_ind.png",
                    selectedIconPath: "../../images/icon/icon_indS.png",
                    selected: true
                  },
                  {
                    pagePath: "../../pages/mine/mine",
                    text: "我的",
                    iconPath: "../../images/icon/icon_mine.png",
                    selectedIconPath: "../../images/icon/icon_mineS.png",
                    selected: false
                  }
                ],
                position: "bottom"
              },
            })
          }
          this.editTabBar();
        }
      },
    })
  },
  //切换tabbar的方法  
  editTabBar: function() {
    var tabbar = this.data.tabbar,
      currentPages = getCurrentPages(),
      _this = currentPages[currentPages.length - 1],
      pagePath = _this.__route__;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (var i in tabbar.list) {
      // tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
    // console.log(tabbar)
  },
  //点击每日登陆
  click_gives() {
    this.getGives();
  },
  //登录好礼领取
  getGives() {
    wx.request({
      url: app.globalData.url + 'api/login/gives/get',
      data: {
        token: app.globalData.token,
        type: 1,
      },
      method: 'POST',
      success: (e) => {
        console.log(e.data.message);
        wx.showToast({
          title: '领取成功',
          icon: 'none'
        })
        this.getTask();
      },
    })
  },
  //领取
  go_receive() {
    var that = this;
    var balance = this.data.balance;
    if (balance.has_resume == "2") {
      wx.showToast({
        title: '简历审核中，暂不能操作',
        icon: 'none',
      })
    } else if (balance.has_resume == "3") {
      wx.showToast({
        title: '简历审核失败，请重新上传',
        icon: 'none',
      })
    } else if (balance.has_resume == "1") {
      var receive_time = this.data.receive_time;
      receive_time = receive_time + 1;
      this.setData({
        receive_time: receive_time
      })
      this.balance();
      if (this.data.receive_time == 1) {
        wx.request({
          // url: app.globalData.url + 'api/invitation/user/receive',
          url: app.globalData.url + 'api/task/send/resume/draw',
          data: {
            token: app.globalData.token,
          },
          method: 'POST',
          success: (e) => {
            wx.showToast({
              title: '领取成功',
              icon: 'none'
            })
            // console.log(e)
            this.balance();
            this.getTask();
          },
        })
      }
    }
  },
  //去简历
  go_resume() {
    let that = this,
      balance = this.data.balance;
    if (balance.has_resume == 0) {
      wx.navigateTo({
        url: '../resume/uploadResume/uploadResume?welfare=true',
      })
    } else if (balance.has_resume == 2) {
      wx.showToast({
        title: '简历正在审核中',
        icon: 'none',
      })
    } else if (balance.has_resume == 3) {
      wx.showModal({
        content: '审核失败，请重新发布',
        showCancel: true,
        confirmText: '重新发布',
        confirmColor: '#d0a76c',
        success: function(e) {
          if (e.confirm) {
            wx.navigateTo({
              url: '../resume/uploadResume/uploadResume?welfare=true',
            })
          }
        },
      })
    }
  },
  //去关注
  go_concern() {
    this.setData({
      flag_concern: true,
    })
  },
  //查看公司
  go_check() {
    wx.navigateTo({
      url: '../mine/anchorWelfare/check/check',
    })
  },
  //去签到
  go_sign() {
    wx.navigateTo({
      url: '../mine/task/index?show=show_check'
    })
  },
  //本人投递
  go_recruit(e) {
    wx.navigateTo({
      url: '../recruit/index/index?flag_falls=true'
    })
  },
  //自己投简历
  selfPost(e) {
    let type = parseInt(e.currentTarget.dataset.type),
      money = this.data.money;
    if (type == 1) {
      //去满足条件
      if (money.send_resume.is_resume == 1) {
        //没简历
        this.setData({
          flag_out: false
        })
      }else{
        //有简历
        wx.navigateTo({
          url: '../recruit/index/index?flag_falls=true'
        })
      }
    } else if (type == 2) {
      //去领取
      if (money.send_resume.apply_status==0){
        wx.navigateTo({
          url: '../mine/anchorWelfare/anchorWelfare_detail/anchorWelfare_detail',
        })
      }else{
        console.log("提现中")
      }
    } else if (type == 3) {
      console.log("已领取")
    }
  },
  //邀请注册
  register(e) {
    let type = parseInt(e.currentTarget.dataset.type),
      money = this.data.money;
    if (type == 1) {
      //去满足条件
      this.click_invite()
    } else if (type == 2) {
      //去领取
      this.drawMoney(1, money.invite_anchor.stage, money.invite_anchor.get_bb)
    } else if (type == 3) {
      console.log("已领取")
    }
  },
  //好友投递
  otherPost(e){
    let type = parseInt(e.currentTarget.dataset.type),
      money = this.data.money;
    if (type == 1) {
      //去满足条件
      wx.showToast({
        title: '未达到领取条件',
        icon: 'none'
      })
    } else if (type == 2) {
      //去领取
      this.drawMoney(2, money.invite_send.stage, money.invite_send.get_bb)
    } else if (type == 3) {
      console.log("已领取")
    }
  },
  //简历被查看
  checked(e){
    let type = parseInt(e.currentTarget.dataset.type),
      money = this.data.money;
    if (type == 1) {
      //去满足条件
      if (money.resume_view.has_total >= money.resume_view.need_total){
        //满足
        this.go_check();
      }else{
        //未满足
        wx.showToast({
          title: '未达到领取条件',
          icon: 'none'
        })
      }
      
    } else if (type == 2) {
      //去领取
      this.drawMoney(3, money.resume_view.stage, money.resume_view.get_bb)
    } else if (type == 3) {
      console.log("已领取")
    }
  },
  //分享链接
  click_url() {
    let that = this;
    let balance = that.data.balance;
    let share_url = balance.share_url;
    let code = balance.code;
    wx.showModal({
      content: '已复制，快去分享给好友吧！',
      showCancel: false,
      confirmText: '好的',
      confirmColor: '#d0a76c',
      success: function (e) {
        // console.log(e)
        if (e.confirm) {
          wx.setClipboardData({
            data: share_url + code,
          })
          that.setData({
            flag_share: true,
          })
        }
      },
    })
  },
  //保存图片
  click_save(e) {
    let that = this;
    // console.log(e)
    wx.getSetting({
      success(res) {
        // 如果没有则获取授权
        if (!res.authSetting['scope.writePhotosAlbum']) {

          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              wx.saveImageToPhotosAlbum({
                filePath: that.data.share_img,
                success() {
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    success() {
                      that.setData({
                        flag_canvas: true,
                      })
                    }
                  })
                },
                fail() {
                  wx.showToast({
                    title: '保存失败',
                    icon: 'none',
                    success() {
                      that.setData({
                        flag_canvas: true,
                      })
                    }
                  })
                }
              })
            },
          })
        } else {
          // 有则直接保存
          that.setData({
            flag_canvas: true,
          })
          wx.saveImageToPhotosAlbum({
            filePath: that.data.share_img,
            success() {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                success() {
                  that.setData({
                    flag_canvas: true,
                  })
                }
              })
            },
            fail() {
              wx.showToast({
                title: '保存失败',
                icon: 'none',
                success() {
                  that.setData({
                    flag_canvas: true,
                  })
                }
              })
            }
          })
        }
      }
    })
  },
  //分享图片
  click_img() {
    this.setData({
      flag_share: true,
      flag_canvas: false,
    })
  },
  //邀请
  click_invite() {
    let that = this;
    wx.showLoading({
      mask: false,
    })
    // var timer = setTimeout(function () {
    //   wx.hideLoading()
    //   clearTimeout(timer)
    // }, 1000)
    that.balance();
    that.createCanvas();
    let balance = this.data.balance;
    if (!balance.code) {
      that.balance();
      that.setData({
        flag_share: false,
      })
    }
    else {
      that.balance();
      that.setData({
        flag_share: false,
      })
    }
  },
  //生成画板
  createCanvas() {
    let that = this;
    //开始画图
    const ctx = wx.createCanvasContext('share_canvas');
    let rpx = that.data.rpx;
    // console.log(rpx)
    ctx.drawImage(that.data.canvas_bg, 0, 0, 280 * rpx, 500 * rpx);
    ctx.drawImage(that.data.qrcode_img, 105 * rpx, 350 * rpx, 70 * rpx, 70 * rpx);
    ctx.draw(true, function (e) {
      // console.log(e)
      //导出
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 280,
        height: 500,
        destWidth: 560,
        destHeight: 1000,
        canvasId: 'share_canvas',
        success: function (res) {
          // console.log(res.tempFilePath)
          that.setData({
            share_img: res.tempFilePath,
          })
          // console.log(res.tempFilePath)
        }
      })
      wx.hideLoading();
    });
  },
  canvas_hide() {
    this.setData({
      flag_canvas: true,
    })
  },
  //发起提现
  drawMoney(type, stage, bb_num) {
    wx.request({
      url: app.globalData.url + 'api/anchor/how/make/money/gain',
      data: {
        token: app.globalData.token,
        type: type,
        stage: stage,
        bb_num
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.makeMoney();
      }
    })
  },
  //提现状态
  withdraw() {
    wx.request({
      url: app.globalData.url + 'api/invitation/apply/condition',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let type = e.data.data.type;
        this.setData({
          type: type
        })
      }
    })
  },
  //点击 提现
  change_withdraw() {
    this.withdraw();
    const type = this.data.type;
    if(type == 7) {
      // wx.showToast({
      //   title: '提现审核中',
      //   icon: 'none',
      // })
    }else{
      wx.navigateTo({
        url: '../mine/anchorWelfare/anchorWelfare_detail/anchorWelfare_detail',
      })
    }
    // this.withdraw();
    // const type = this.data.type;
    // if (type == 0) {
    //   wx.navigateTo({
    //     url: '../mine/anchorWelfare/anchorWelfare_detail/anchorWelfare_detail',
    //   })
    // } else if (type == 2) {
    //   wx.showToast({
    //     title: '简历审核中',
    //     icon: 'none',
    //   })
    // } else if (type == 7) {
    //   wx.showToast({
    //     title: '提现审核中',
    //     icon: 'none',
    //   })
    // } else {
    //   this.setData({
    //     flag_ruleTwo: false,
    //   })
    // }
  },
  //隐藏
  click_hide() {
    this.setData({
      flag_rule: true,
      flag_ruleTwo: true,
    })
  },
  //隐藏弹框
  modal_hide() {
    this.setData({
      flag_share: true,
      flag_check: true,
      flag_concern: false,
      flag_out: true,
      flag_canvas:true,
    })
  },
  //复制微信、公众号
  click_copy_wechat(e) {
    // console.log(e)
    let wechat = e.currentTarget.dataset.wechat;
    wx.setClipboardData({
      data: wechat,
      success: function(e) {}
    })
  },
  //每日任务
  getTask() {
    wx.request({
      url: app.globalData.url + 'api/task/everyday/mission',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let signInfo = e.data.data;
        this.setData({
          signInfo: signInfo
        })
      },
    })
  },
  //获取我的额度 等信息
  balance() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/invitation/my/balance',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function (e) {
        // console.log(e)
        let balance = e.data.data;
        let code = e.data.data.code,
          canvas_url = e.data.data.invitation_img_url,
          qrcode_url = e.data.data.qrcode_url;
        // console.log(canvas_url)
        // console.log(qrcode_url)
        that.setData({
          balance: balance,
          canvas_url: canvas_url,
          qrcode_url: qrcode_url,
        })
        that.download_qrcode_img();
        that.download_canvas_bg();
      },
    })
  },
  //downqrcode
  download_qrcode_img() {
    wx.downloadFile({
      url: this.data.qrcode_url,
      success: (e) => {
        // console.log(e.tempFilePath)
        this.setData({
          qrcode_img: e.tempFilePath,
        })
        wx.hideLoading()
      }
    })
  },
  download_canvas_bg() {
    wx.downloadFile({
      url: this.data.canvas_url,
      success: (e) => {
        // console.log(e.tempFilePath)
        this.setData({
          canvas_bg: e.tempFilePath,
        })
        wx.hideLoading()
      }
    })
  },
  // 获取规则
  getRule() {
    wx.request({
      url: app.globalData.url + 'api/broker/lift/rule',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        let welfare = e.data.data.welfare,
          rule = e.data.data.rule,
          upgrade = e.data.data;
        this.setData({
          upgrade: upgrade,
        })
      }

    })
  },
  //用户基础信息接口
  user_info: function() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        var info = e.data.data;

        that.setData({
          info: info
        })
      },

    })
  },
  //主播赚钱接口
  makeMoney() {
    wx.request({
      url: app.globalData.url + 'api/anchor/how/make/money',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        this.setData({
          money: e.data.data
        })
      },

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getTab();
    var role = wx.getStorageSync('reg_register_role');
    wx.getSystemInfo({
      success:(e)=> {
        const rpx = e.windowWidth / 375;
        this.setData({
          rpx: rpx,
        })
      }
    })
    this.setData({
      role: role,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.user_info();
    this.getTask();
    this.balance();
    this.getRule();
    this.makeMoney();
    this.withdraw();
    //授权检测
    if (!(wx.getStorageSync('token'))) {
      wx.redirectTo({
        url: '../author/author',
      })
      // console.log('跳转')
    } else {
      if (wx.getStorageSync('reg_register_role') == 0) {
        wx.redirectTo({
          url: '../selectIdentity/selectIdentity',
        })
      } else { }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})